package com.ymm.lulutong.domain.impl;

import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperAddressDomainService;
import com.ymm.lulutong.domain.utils.AddressUtils;
import com.ymm.lulutong.entity.Address;
import com.ymm.lulutong.mapper.ShipperAddressMapper;
import com.ymm.lulutong.request.SaveAddressRequest;
import com.ymm.lulutong.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ShipperAddressDomainServiceImpl implements ShipperAddressDomainService {
    @Resource
    ShipperAddressMapper shipperAddressMapper;

    @Override
    public Result<Boolean> shipperSaveOftenAddress(SaveAddressRequest saveAddressRequest) {
        Result<Boolean> parametersCheckResult = saveAddressRequestParametersCheck(saveAddressRequest);
        if (Objects.isNull(parametersCheckResult)) {
            log.info("输入参数错误");
            return Result.fail(false, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        if (!parametersCheckResult.isSuccess()) {
            return Result.fail(false, parametersCheckResult.getCode(), parametersCheckResult.getMessage());
        }
        Address address = new Address();
        try {
            address.setShipperId(saveAddressRequest.getShipperId());
            address.setAddressProv(saveAddressRequest.getAddressProv());
            address.setAddressCity(saveAddressRequest.getAddressCity());
            address.setAddressRegion(saveAddressRequest.getAddressRegion());
            address.setAddressSrc(saveAddressRequest.getAddressSrc());
            address.setAddressLat(saveAddressRequest.getAddressLat());
            address.setAddressLon(saveAddressRequest.getAddressLon());
            address.setAddressType(0);
            address.setAddressCreatedTime(new Date());
            address.setAddressUpdateTime(new Date());
            address.setIsDeleted(0);
            shipperAddressMapper.saveOftenAddress(address);
        } catch (Exception e) {
            log.error("数据库操作错误");
            e.printStackTrace();
            return Result.fail(false, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
        }
        return Result.success(true);
    }

    @Override
    public Result<Boolean> shipperRemoveAddress(Long addressId) {
        if (Objects.isNull(addressId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        shipperAddressMapper.deleteAddress(addressId);
        return Result.success(true);
    }

    @Override
    public Result<List<Address>> shipperListAddress(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        List<Address> addresses = shipperAddressMapper.listAllAddress(shipperId);
        System.out.println(addresses);
        return Result.success(addresses);
    }

    /**
     * 校验 保存地址请求 参数
     *
     * @param saveAddressRequest 保存地址请求
     * @return 是否合法
     */
    private Result<Boolean> saveAddressRequestParametersCheck(SaveAddressRequest saveAddressRequest) {
        if (Objects.isNull(saveAddressRequest) || Objects.isNull(saveAddressRequest.getShipperId()) || Objects.isNull(saveAddressRequest.getAddressType())) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR.getCode(), "参数为空");
        }
        if (!AddressUtils.latLongCheck(saveAddressRequest.getAddressLat(), saveAddressRequest.getAddressLon())) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR.getCode(), "位置错误！");
        }
        if (Objects.isNull(saveAddressRequest.getAddressSrc()) || saveAddressRequest.getAddressSrc().length() == 0 || !AddressUtils.provCityRegionCheck(saveAddressRequest.getAddressProv(), saveAddressRequest.getAddressCity(), saveAddressRequest.getAddressRegion())) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR.getCode(), "位置错误");
        }
        return Result.success(true);
    }
}
